﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.ZilLionOss.Domain.Entitys;
using ZilLion.Service.ZilLionOss.Domain.Repositorys.Interface;

namespace ZilLion.Service.ZilLionOss.Domain.Repositorys
{
    public class OssCompanyMoudleReponsitory : SqlServerRespository<OssCompanyMoudleEntity>,
        IOssCompanyMoudleReponsitory
    {
        public OssCompanyMoudleReponsitory()
        {
            Context = new OssContext();
        }

        public IList<OssCompanyMoudleEntity> GetAllCompanyMoudle()
        {
            return GetAll().ToList();
        }

        public IList<OssCompanyMoudleEntity> GetCompanyMoudleByCompanyKey(string companyKey)
        {
            var querysql = DapperHelper.CreateColSqlByMap<OssCompanyMoudleEntity>();
            var sql = $@"SELECT {querysql} FROM oss_company_moudle
            WHERE zlcompanykey=@companyKey";
            var result = GetList(sql, new {companyKey}).ToList();
            return result;
        }

        public IList<OssCompanyMoudleEntity> GetCompanyMoudleByKey(string companyKey, string moudleKey)
        {
            var querysql = DapperHelper.CreateColSqlByMap<OssCompanyMoudleEntity>();
            var sql = $@"SELECT {querysql} FROM oss_company_moudle
            WHERE zlcompanykey=@companyKey and moudlekey=@moudleKey";
            var result = GetList(sql, new {companyKey, moudleKey}).ToList();
            return result;
        }

        public void AddMoudle(OssCompanyMoudleEntity entity, UnitOfWork unit = null)
        {
            Add(entity, unit);
        }

        public void EditMoudle(OssCompanyMoudleEntity entity, UnitOfWork unit = null)
        {
            Modify(entity, unit);
        }

        public void RemoveMoudle(OssCompanyMoudleEntity entity, UnitOfWork unit = null)
        {
            Remove(entity, unit);
        }

        public void RemoveMoudleByCompanyKey(string companyKey, UnitOfWork unit = null)
        {
            DapperHelper.Execute("delete from oss_company_moudle where zlcompanykey=@companyKey", Context, unit,
                new {companyKey});
        }
    }
}